﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using CardRegisterCollection.Model;
using CardRegisterCollection.Utility.Enum;

namespace CardRegisterCollection.BLL
{
    /// <summary>
    /// user
    /// </summary>
    public partial class User
    {
        private readonly DAL.User dal = new DAL.User();
        private readonly DAL.UserRole dalUserRole = new DAL.UserRole();
        private readonly DAL.RoleMenu dalRoleMenu = new DAL.RoleMenu();

        public User()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string strWhere)
        {
            return dal.Exists(strWhere);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.User model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.User model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {
            dalUserRole.Delete(ID);
            return dal.Delete(ID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            return dal.DeleteList(IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.User GetModel(int ID)
        {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Model.User GetModelByCache(int ID)
        {
            var objModel = dal.GetModel(ID);

            return (Model.User)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.UserExtension> GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.User> DataTableToList(DataTable dt)
        {
            List<Model.User> modelList = new List<Model.User>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.User model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion BasicMethod

        #region ExtensionMethod

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username">账号</param>
        /// <param name="password">账号</param>
        /// <returns></returns>
        public LoginRltEnum Login(string username, string password)
        {
            var userInfo = dal.Login(username);
            if (null == userInfo) return LoginRltEnum.Invalid;
            if (!userInfo.Password.Equals(password)) return LoginRltEnum.PasswordError;

            var userRoles = dalUserRole.GetListByUserId(userInfo.ID);
            if (null == userRoles || userRoles.Count == 0) return LoginRltEnum.Unauthorized;

            var roleIds = userRoles.Select(_ => _.RoleId).ToList();
            var roleMenus = dalRoleMenu.GetMenuByRoleId(roleIds);

            CurrentLoginInfo.UserInfo = userInfo;
            CurrentLoginInfo.Roles = userRoles;
            CurrentLoginInfo.Menus = roleMenus;

            return LoginRltEnum.Normal;
        }

        #endregion  ExtensionMethod
    }
}

